package jing.upnp.factory;

import android.content.Context;

import java.io.InputStream;

import jing.upnp.factory.loader.DocumentLoader;
import jing.upnp.factory.loader.DocumentLoaderFactory;
import jing.upnp.factory.loader.DocumentLoaderType;

/**
 * Created by ouyang on 15-9-11.
 */
public class DocumentManager {

    private static final int RETRY_TIMES = 3;

    private DocumentManager() {
    }

    public static InputStream getDocument(Context context, String url) {
        InputStream is = null;

        do {
            DocumentLoader loader = null;

            loader = DocumentLoaderFactory.create(DocumentLoaderType.CACHE);
            is = loader.getStream(context, url);
            if (is != null) {
                break;
            }

//            loader = DocumentLoaderFactory.create(DocumentLoaderType.HTTP_OK);
//            for (int i = 0; i < RETRY_TIMES; ++i) {
//                is = loader.getStream(context, url);
//                if (is != null) {
//                    break;
//                }
//            }
//
//            if (is != null) {
//                break;
//            }

            loader = DocumentLoaderFactory.create(DocumentLoaderType.HTTP);
            for (int i = 0; i < RETRY_TIMES; ++i) {
                is = loader.getStream(context, url);
                if (is != null) {
                    break;
                }
            }

            if (is != null) {
                break;
            }
        } while (false);

        return is;
    }
}